package org.micropayment.model;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author jj
 */
@Entity
@Table(name = "Transaction")
@NamedQueries({
    @NamedQuery(name = "Transaction.findAll", query = "SELECT t FROM Transaction t"),
    @NamedQuery(name = "Transaction.findById", query = "SELECT t FROM Transaction t WHERE t.id = :id"),
    @NamedQuery(name = "Transaction.findByRequestDate", query = "SELECT t FROM Transaction t WHERE t.requestDate = :requestDate"),
    @NamedQuery(name = "Transaction.findByAccessDate", query = "SELECT t FROM Transaction t WHERE t.accessDate = :accessDate"),
    @NamedQuery(name = "Transaction.findByAmount", query = "SELECT t FROM Transaction t WHERE t.amount = :amount"),
    @NamedQuery(name = "Transaction.findByCurrency", query = "SELECT t FROM Transaction t WHERE t.currency = :currency")})
public class Transaction implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "requestDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date requestDate;
    @Column(name = "accessDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date accessDate;
    @Basic(optional = false)
    @Column(name = "amount")
    private long amount;
    @Basic(optional = false)
    @Column(name = "currency")
    @Enumerated(value = EnumType.STRING)
    private CurrencyCode currency;
    @Lob
    @Column(name = "details")
    private String details;
    @JoinColumn(name = "requestToken", referencedColumnName = "tokenValue")
    @OneToOne(optional = false)
    private Token requestToken;
    @JoinColumn(name = "accessToken", referencedColumnName = "tokenValue")
    @OneToOne(optional = true)
    private Token accessToken;
    @JoinColumn(name = "accountToId", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Account accountToId;
    @JoinColumn(name = "accountFromId", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Account accountFromId;

    public Transaction() {
    }

    public Transaction(Integer id) {
        this.id = id;
    }

    public Transaction(Integer id, Date requestDate,
            Date accessDate,long amount, CurrencyCode currency) {
        this.id = id;
        this.requestDate = requestDate;
        this.accessDate = accessDate;
        this.amount = amount;
        this.currency = currency;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Date getRequestDate() {
        return requestDate;
    }

    public void setRequestDate(Date requestDate) {
        this.requestDate = requestDate;
    }

        public Date getAccessDate() {
        return accessDate;
    }

    public void setAccessDate(Date accessDate) {
        this.accessDate = accessDate;
    }

    public long getAmount() {
        return amount;
    }

    public void setAmount(long amount) {
        this.amount = amount;
    }

    public CurrencyCode getCurrency() {
        return currency;
    }

    public void setCurrency(CurrencyCode currency) {
        this.currency = currency;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public Token getRequestToken() {
        return requestToken;
    }

    public void setRequestToken(Token requestToken) {
        this.requestToken = requestToken;
    }

    public Token getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(Token accessToken) {
        this.accessToken = accessToken;
    }

    public Account getAccountToId() {
        return accountToId;
    }

    public void setAccountToId(Account accountToId) {
        this.accountToId = accountToId;
    }

    public Account getAccountFromId() {
        return accountFromId;
    }

    public void setAccountFromId(Account accountFromId) {
        this.accountFromId = accountFromId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Transaction)) {
            return false;
        }
        Transaction other = (Transaction) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.micropayment.model.Transaction[id=" + id + "]";
    }

}
